package com.canbot.u05.adapter.dance

import android.content.Context
import android.support.v4.view.MotionEventCompat
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.canbot.u05.R
import com.canbot.u05.bean.dance.ActionBean

class CustomActionAdapter(internal val mContext: Context, private var mbrowList: List<ActionBean>) : RecyclerView.Adapter<CustomActionAdapter.MyViewHolder>() {

        private var index: Int = 0
        private var itemOnClickListener: ItemOnClickListener? = null
        private var mDragStartListener:OnStartDragListener? = null
        inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        }
        fun setData(actionLists:List<ActionBean>){
                this.mbrowList = actionLists
                notifyDataSetChanged()
        }

        override fun getItemViewType(position: Int): Int {
                return position
        }
        fun addAction(actionBean: ActionBean):View{
                val textView = TextView(mContext)
                if (actionBean.duration>30){
                        textView.setText("${actionBean.name} ${actionBean.duration}s")
                }else{
                        textView.setText("${actionBean.duration}s")
                }

                textView.setTextColor(mContext.resources.getColor(R.color.white,null))
                textView.setTextSize(14f)
                textView.setGravity(Gravity.CENTER)
                textView.background = mContext.resources.getDrawable(R.drawable.dance_action_play_item_shape,null)
                val layoutParams = LinearLayout.LayoutParams((actionBean.duration * 8 + 5) - 2, 68)
                layoutParams.setMargins(0,0,2,0)
                textView.layoutParams = layoutParams
                return textView
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

                return MyViewHolder(addAction(mbrowList.get(viewType)))
        }

        fun addItemOnClickListener(itemOnClickListener: ItemOnClickListener){
                this.itemOnClickListener = itemOnClickListener
        }
        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
                holder.setIsRecyclable(false)
                holder.itemView.setOnClickListener {
                        itemOnClickListener?.onClick(position)
                }
                holder.itemView.setOnTouchListener(object :View.OnTouchListener{
                        override fun onTouch(v: View, event: MotionEvent): Boolean {
                                if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
                                        mDragStartListener?.onStartDrag(holder);
                                }
                                return false
                        }

                })
        }
        interface OnStartDragListener{
                fun onStartDrag(holder:RecyclerView.ViewHolder)
        }
        fun setOnDragStartListener(mDragStartListener: OnStartDragListener) {
                this.mDragStartListener = mDragStartListener
        }
        override fun getItemId(i: Int): Long {
                return i.toLong()
        }

        override fun getItemCount(): Int {
                return mbrowList.size
        }

        fun setIndex(i: Int) {
                index = i
        }

        interface ItemOnClickListener{
                fun onClick(index:Int)
        }

}
